// pages/car/car.js
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var xrSdk = require('../../../vendor/hope-sdk/index.js');
var config = require('../../../config.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchKey: "", //搜索关键词
    width: 200, //header宽度
    height: 64, //header高度
    inputTop: 0, //搜索框距离顶部距离
    arrowTop: 0, //箭头距离顶部距离
    dropScreenH: 0, //下拉筛选框距顶部距离
    
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,
    noContent: false,
    carList: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let obj = wx.getMenuButtonBoundingClientRect();
    this.setData({
      width: obj.left,
      height: obj.top + obj.height + 8,
      inputTop: obj.top + (obj.height - 30) / 2,
      arrowTop: obj.top + (obj.height - 32) / 2
    }, () => {
      wx.getSystemInfo({
        success: (res) => {
          this.setData({
            //略小，避免误差带来的影响
            dropScreenH: this.data.height * 750 / res.windowWidth, // + 186,
            drawerH: res.windowHeight - res.windowWidth / 750 * 100 - this.data.height
          })
        }
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onPullDownRefresh();
  },
  onClick:function(e){
    wx.navigateTo({
      url: "../add/add?source=1"
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initData();
    this.initList();
  },
  initData: function () {
    this.setData({
      carList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true,
      noContent: false
    })
  },
  initList: function () {
    var that = this;
    var params = that.getParams();
    xrRequest.request(app,{
      url: config.service.carList,
      login: true,
      data: params,
      success: function (res) {
        that.setData({
          carList: that.data.carList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        // 没有更多
        if (res.data.data.has_more == false &&
          that.data.carList.length > 0) {
          that.setData({
            pullUpOn: false
          })
        }
        // 空数据
        that.setData({
          noContent: that.data.carList.length == 0
        });
        wx.stopPullDownRefresh();
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    });
  },
  getParams() {
    var that = this;
    var params = {
      page: that.data.pageIndex,
      page_size: 10,
      keyword: that.data.searchKey
    };
    return params;
  },
  onReachBottom: function () {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initList();
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  input: function (e) {
    //console.log(e);
    this.setData({
      searchKey: e.detail.value
    })
  },
  search: function (e) {
    console.log(e);
    this.setData({
      searchKey: e.detail.value
    });
    this.onPullDownRefresh();
  },
  // 详情
  detail:function(e){
    wx.navigateTo({
      url: "../view/view?id=" + e.currentTarget.dataset.id
    })
  },
  setDefault:function(e){
    var car_id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '确定操作吗',
      content: '确定设置当前车辆为默认车辆吗？',
      success(res) {
        if (res.confirm) {
          that.setDefaultHandler(car_id);
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    }); 
  },
  setDefaultHandler: function (car_id){
    var that = this;
    xrRequest.request(app,{
      url: config.service.setDefault,
      login: true,
      data: {
        id: car_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          that.onPullDownRefresh();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  // 安装
  install:function(e){
    var id = e.currentTarget.dataset.id;
    xrSdk.cookie.set('car_id', id);
    wx.navigateTo({
      url: '../../activity/apply/apply'
    });
  },
  back: function () {
    wx.navigateBack()
  }
})